// 用后序遍历和中序遍历序列构造二叉树
function buildTree(inorder, postorder) {
    if (!postorder.length) {
        return null
    }
    // 后序遍历可以拿到根节点的值
    let val = postorder.pop()
    // 中序遍历可以拿到根节点的索引位置
    let index = inorder.indexOf(val)
    let root = new TreeNode(val)
    root.left = buildTree(inorder.slice(0, index), postorder.slice(0, index))
    root.right = buildTree(inorder.slice(index + 1), postorder.slice(index))
    return root

}
function TreeNode(val) {
    this.val = val
    this.left = null
    this.right = null
}

let inorder = [9,3,15,20,7]
let postorder = [9,15,7,20,3]
console.log(buildTree(inorder, postorder))